//
//  ThirdViewController.swift
//  zhichidata
//
//  Created by joke on 15/12/22.
//  Copyright © 2015年 joke. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON
import Kanna


class ThirdViewController: UIViewController {
    
    @IBOutlet weak var refreshButton: UIButton!
    @IBOutlet weak var date1: UILabel!
    @IBOutlet weak var date2: UILabel!
    @IBOutlet weak var date3: UILabel!
    @IBOutlet weak var date4: UILabel!
    @IBOutlet weak var date5: UILabel!
    @IBOutlet weak var data1: UILabel!
    @IBOutlet weak var data2: UILabel!
    @IBOutlet weak var data3: UILabel!
    @IBOutlet weak var data4: UILabel!
    @IBOutlet weak var data5: UILabel!
    @IBOutlet weak var ipOn: UILabel!
    @IBOutlet weak var ipOff: UILabel!
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "three"
        //data121()
//        let refreshControl = UIRefreshControl()
//        let tableView = UITableView()
//        tableView.frame = view.frame
//        refreshControl.addTarget(self, action: Selector("refreshView"), forControlEvents: .ValueChanged)
//        tableView.addSubview(refreshControl)
//        self.view.addSubview(tableView)
        
//        self.dataReceive()
        
        refreshButton.addTarget(self, action: Selector("dataReceive"), forControlEvents:.TouchUpInside)
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    override func viewDidAppear(animated: Bool) {
        print("the data page shows")
        self.dataReceive()
    }
    
    func refreshView(refreshControl:UIRefreshControl) {
        refreshControl.attributedTitle = NSAttributedString(string: "Refresh")
        refreshControl.attributedTitle = NSAttributedString(string: "Last updated on" + NSData().description)
        
        refreshControl.endRefreshing()
    }
    
    func dataDaili(){
        Alamofire.request(.GET, "http://www.httpdaili.com/api.asp?ddbh=w9653542&sl=10000&noinfo=true").responseData(){
            (data) in
            let value = JSON(data.result.value!)
            print(value)
        }
    }
    
    
    func data121(){
        
        Alamofire.request(.GET, "http://121.40.173.122:2222/index.asp?pass=kJuinbjqe8nvhNjh39ioqaz").responseString(){
            (data) in
            //print(data.result.value)
            //print(data)
            let doc = Kanna.HTML(html: data.result.value!, encoding: NSUTF8StringEncoding)
            //let hr = doc?.xpath("//hr")
            print(doc?.innerHTML)
        }
    }
    
    func dataReceive(){
        Alamofire.request(.GET, "http://zhichidata.oicp.net:8022/json").responseJSON() {
            (data) in
//            print(data)
            switch data.result {
            case .Success(_):
                if let value = data.result.value{
                    let json = JSON(value)
                    print(2)
                    self.data1.text = json["ip1"].stringValue
                    self.data2.text = json["ip2"].stringValue
                    self.data3.text = json["ip3"].stringValue
                    self.data4.text = json["ip4"].stringValue
                    self.data5.text = json["ip5"].stringValue
                    self.date1.text = json["data1"].stringValue
                    self.date2.text = json["data2"].stringValue
                    self.date3.text = json["data3"].stringValue
                    self.date4.text = json["data4"].stringValue
                    self.date5.text = json["data5"].stringValue
                    self.ipOn.text = json["ipOn"].stringValue
                    self.ipOff.text = json["ipOff"].stringValue
                }
            case .Failure(let error):
                print(error)
            }
            
        }

    }
    
}

